package org.jeecg.modules.performance.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;

import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.performance.Model.PerformanceModel;
import org.jeecg.modules.performance.entity.PerformanceBaseConfig;
import org.jeecg.modules.performance.entity.PerformanceConfig;
import org.jeecg.modules.performance.service.IPerformanceBaseConfigService;
import org.jeecg.modules.performance.service.IPerformanceConfigService;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 绩效
 * @Author: jeecg-boot
 * @Date:   2021-04-14
 * @Version: V1.0
 */
@Slf4j
@Api(tags="绩效")
@RestController
@RequestMapping("/performance/performanceConfig")
public class PerformanceConfigController extends JeecgController<PerformanceConfig, IPerformanceConfigService> {
	@Autowired
	private IPerformanceConfigService performanceConfigService;
	@Autowired
	private IPerformanceBaseConfigService performanceBaseConfigService;
	 @Autowired
	private ISysDictItemService sysDictItemService;
	 @Autowired
	 private ISysDictService  sysDictService;


	@AutoLog(value = "绩效-绩效配置页面")
	@ApiOperation(value="绩效-绩效配置页面", notes="绩效-绩效配置页面")
	@GetMapping(value = "/list")
	public Result<?> queryPageList() {
		List<PerformanceConfig> performanceConfigList = performanceConfigService.list();
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("bigId",performanceConfigList.get(performanceConfigList.size()-1).getId());
		Map<String, List<PerformanceConfig>> listMap = performanceConfigList.stream().collect(Collectors.groupingBy(PerformanceConfig::getType));
		resultMap.put("listMap" ,listMap );

		Result<List<SysDictItem>> result = new Result<List<SysDictItem>>();
		SysDict dictParm = new SysDict();
		dictParm.setDictCode("special");
		List<SysDict> dictList = sysDictService.list(new QueryWrapper<SysDict>(dictParm));

		if(null != dictList && dictList.size() > 0){
			SysDictItem sysDictItem = new SysDictItem();
			sysDictItem.setDictId(dictList.get(0).getId());
			QueryWrapper<SysDictItem> queryWrapper = new QueryWrapper<SysDictItem>(sysDictItem);
			queryWrapper.orderByAsc("sort_order");
			List<SysDictItem> dictItemList = sysDictItemService.list(queryWrapper);
			for (SysDictItem dictItem : dictItemList) {
				if(dictItem.getItemText().equals("union_write")){
					dictItem.setItemText("联名撰稿计分方式");
				}
				if(dictItem.getItemText().equals("fix_salary")){
					dictItem.setItemText("固定薪资是否参与绩效考评");
				}
			}
			result.setSuccess(true);
			result.setResult(dictItemList);
			resultMap.put("specialList",dictItemList);

		}
		return Result.ok(resultMap);
	}

	/**
	 * 添加
	 *
	 * @param performanceConfig
	 * @return
	 */
	@AutoLog(value = "绩效-添加")
	@ApiOperation(value="绩效-添加", notes="绩效-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PerformanceConfig performanceConfig) {
		performanceConfigService.save(performanceConfig);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param performanceModel
	 * @return
	 */
	@AutoLog(value = "绩效-编辑")
	@ApiOperation(value="绩效-编辑", notes="绩效-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PerformanceModel performanceModel) {
		performanceConfigService.editPerformances(performanceModel.getPerformanceConfigList());
		for (SysDictItem sysDictItem : performanceModel.getSysDictItemList()) {
			sysDictItem.setItemText(null);
		}
		sysDictItemService.updateBatchById(performanceModel.getSysDictItemList());
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "绩效-通过id删除")
	@ApiOperation(value="绩效-通过id删除", notes="绩效-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		performanceConfigService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "绩效-批量删除")
	@ApiOperation(value="绩效-批量删除", notes="绩效-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.performanceConfigService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "绩效-通过id查询")
	@ApiOperation(value="绩效-通过id查询", notes="绩效-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PerformanceConfig performanceConfig = performanceConfigService.getById(id);
		return Result.ok(performanceConfig);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param performanceConfig
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, PerformanceConfig performanceConfig) {
      return super.exportXls(request, performanceConfig, PerformanceConfig.class, "绩效");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, PerformanceConfig.class);
  }

}
